import { DataTypes, Model, Sequelize } from "sequelize";

/**
 * 文档协作关系模型接口
 */
export interface DocumentCollaborationAttributes {
  id?: number;
  document_id: string;
  user_id: string;
  permission: "read" | "write" | "admin";
  invited_by: string;
  invited_at?: Date;
  responded_at?: Date;
  created_at?: Date;
  updated_at?: Date;
}

/**
 * 文档协作关系模型类
 */
export class DocumentCollaboration
  extends Model<DocumentCollaborationAttributes>
  implements DocumentCollaborationAttributes {
  public id!: number;
  public document_id!: string;
  public user_id!: string;
  public permission!: "read" | "write" | "admin";
  public invited_by!: string;
  public invited_at!: Date;
  public responded_at!: Date;
  public created_at!: Date;
  public updated_at!: Date;

  // 关联属性
  public readonly document?: any;
  public readonly collaborator?: any;
  public readonly inviter?: any;
}

/**
 * 初始化文档协作关系模型
 */
export function initDocumentCollaborationModel(sequelize: Sequelize): typeof DocumentCollaboration {
  DocumentCollaboration.init(
    {
      id: {
        type: DataTypes.BIGINT,
        primaryKey: true,
        autoIncrement: true,
        comment: "协作关系ID，主键自增"
      },
      document_id: {
        type: DataTypes.UUID,
        allowNull: false,
        comment: "文档ID，关联documents表"
      },
      user_id: {
        type: DataTypes.BIGINT,
        allowNull: false,
        comment: "协作用户ID，关联users表"
      },
      permission: {
        type: DataTypes.ENUM("read", "write", "admin"),
        allowNull: false,
        defaultValue: "write",
        comment: "权限级别：read-只读，write-编辑，admin-管理员"
      },
      invited_by: {
        type: DataTypes.BIGINT,
        allowNull: false,
        comment: "邀请人ID，关联users表"
      },
      invited_at: {
        type: DataTypes.DATE,
        defaultValue: DataTypes.NOW,
        comment: "邀请时间"
      },
      responded_at: {
        type: DataTypes.DATE,
        allowNull: true,
        comment: "响应时间"
      },
      created_at: {
        type: DataTypes.DATE,
        defaultValue: DataTypes.NOW,
        comment: "创建时间"
      },
      updated_at: {
        type: DataTypes.DATE,
        defaultValue: DataTypes.NOW,
        comment: "更新时间"
      }
    },
    {
      sequelize,
      tableName: "document_collaborations",
      modelName: "DocumentCollaboration",
      timestamps: true,
      underscored: true,
      indexes: [
        {
          fields: ["document_id"],
          name: "idx_document_id"
        },
        {
          fields: ["user_id"],
          name: "idx_user_id"
        },
        {
          fields: ["document_id", "user_id"],
          name: "idx_document_user",
          unique: true
        }
      ],
      comment: "文档协作关系表"
    }
  );

  return DocumentCollaboration;
}

/**
 * 定义DocumentCollaboration模型关联关系
 */
export function defineDocumentCollaborationAssociations() {
  const { User } = require("./index");
  const { Document } = require("./Document");

  // DocumentCollaboration属于Document
  DocumentCollaboration.belongsTo(Document, {
    foreignKey: "document_id",
    as: "document"
  });

  // DocumentCollaboration属于User（协作者）
  DocumentCollaboration.belongsTo(User, {
    foreignKey: "user_id",
    as: "collaborator"
  });

  // DocumentCollaboration属于User（邀请者）
  DocumentCollaboration.belongsTo(User, {
    foreignKey: "invited_by",
    as: "inviter"
  });
}
